package d;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-28
 * Time: 8:44
 */
public class TcpEchoclient {
    private Socket socket = null;
    public TcpEchoclient(String serverIP , int serverPort) throws IOException {
        socket = new Socket(InetAddress.getByName(serverIP),serverPort);
    }
    public void start() throws IOException {
        System.out.println("客户端启动!");
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
           Scanner clientResponse = new Scanner(inputStream);
            while (true){
                System.out.print("请输入要传输的数据:");
                String request = scanner.next();
                request += "\n";
                outputStream.write(request.getBytes());
                String response = clientResponse.next();
                System.out.println(response);



            }
        }

    }
    public static void main(String[] args) throws IOException {
        TcpEchoclient tcpEchoclient = new TcpEchoclient("127.0.0.1",9090);
        tcpEchoclient.start();

    }
}
